<?php
namespace app\api\controller;

use think\facade\Db;
use think\facade\Request;
use app\common\model\UserSet;
use app\common\model\CardsTransCredit;

/**
 * 2025-04-28 huangzhen
 * 异步处理银3 接收数据
 * Class TimerCards
 * @package app\api\controller
 */
class ActionLssApi
{
    public function __construct()
    {
        $params = Request::param();
        if(empty($params)){
            exit('Welcome to cardEcho bank 3 Api');
        }else{
            $token = $params['token'];
            //4fe1fa0547648373d1bf912546e0da86
            if($token !=md5(md5('cardEcho') . md5(2025))){
                exit('Welcome to cardEcho bank 3 Api !');
            }
        }
    }

    /**
     * 2025-02-28 为新一轮更新，
     * is_action  0-新数据 | 1-已处理 | 2-处理失败 | 3-已处理(重复值) | 4-已操作
     * 已操作补充说明:不存在的类型出现,说明这条推送已看过，后期再处理)
     */
    public function actionApi()
    {

        //2025-8-31 处理adjusted，terminated两种非消费的类型
        Db::name('api_accept_lss')->where("is_action = 0 
                and (eventType='purchase.card.adjusted' or eventType='purchase.card.terminated')")
            ->update(['is_action' => 4]);

        $start_id = 21812;  //2025-04-28更新 以上传时最新ID开始

        $list = Db::name('api_accept_lss')
            ->where("is_action=0 and id >{$start_id} ")
            ->order('id asc')
            ->limit(20)->select()->toArray();
        $ctime      = getUtcTime();
        foreach ($list as $k=>$v) {
            $old_id = $v['id'];
            $user_id = $v['user_id'];
            $card_num = $v['card_num'];
            $amount = $v['amount'];
            $availableBalance = $v['availableBalance'];
            $tran_guid = $v['tran_guid'];
            $eventType = $v['eventType'];
            $eventTime = $v['eventTime'];
            $shopping = $v['shopping'];
            $log_ctime = $v['ctime'];
            $appCode    = $v['app_code'] ?? '';

            $lss_time  = date('Y-m-d H:i:s',strtotime($eventTime . ' -5 hour'));
            $flag = 'N'; //跟银1统一

            $json_data          = json_decode($v['json_data'], true);
            $currencyCode       = $json_data['data']['authorizationCurrencyCode'] ?? '';
            $cardAcceptorCountry= $json_data['data']['cardAcceptorCountry'] ?? '';
            $mcc                = $json_data['data']['cardAcceptorMcc'] ?? '';
            $mid                = $json_data['data']['cardAcceptorMid'] ?? '';
            $decline_error      = $json_data['data']['authMessage'] ?? '';

            try {
                //①排除重复日志
                $check_list = Db::name('api_accept_lss')
                    ->where("id >{$start_id} and tran_guid='{$tran_guid}'")
                    ->order('id asc')
                    ->select()->toArray();
                //获取第一条日志，取得操作状态is_action的值
                $first_data = reset($check_list)['is_action'];
                //不等于0，不是新数据,且已经处理过,只标记
                if ($first_data != 0) {
                    //第一条已经处理过，那么其他重复的更新为 3-已处理(重复值)
                    if (count($check_list) > 1) {
                        Db::name('api_accept_lss')->where(['id' => $old_id])
                            ->update(['is_action' => 3]);
                    }
                }
                else {
                    Db::startTrans();
                    try {
                        $card_list = Db::name('cards')->where(['card_num' => $card_num])->find();
                        $card_token = $card_list['card_token'];
                        $card_status = $card_list['status'];
                        $card_id = $card_list['id'];
                        $card_balance_total = $card_list['balance_account'];
                        $card_balance = $card_list['balance'];

                        //更新推送后的最新卡余额
                        Db::name('cards')->where(['id' => $card_id])->update(['balance' => $availableBalance]);

                        $user_set   = Db::name('user_set')->where(['user_id'=>$user_id])->find();
                        $server_zero_auth = $user_set['zero_auth'] ?? 0.2;
                        $server_facebook  = $user_set['facebook'] ?? 0.3;
                        $server_declined  = $user_set['declined'] ?? 0.2;

                        $user_list  = Db::name('user')->where(['id'=>$user_id])->find();
                        $user_balance = $user_list['balance'];
                        $username   = $user_list['username'];

                        $correctiveAction= "";

                        //关键字豁免用户
                        $keyword_exempt_users = getSetting('keyword_exempt_users');
                        $keyword_exempt_users_array = explode(',' , $keyword_exempt_users);
                        $key_users = [];
                        foreach ($keyword_exempt_users_array as $k_users=>$v_users) {
                            $key_users[] = Db::name('user')->where(['username' => $v_users])->value('id');
                        }
                        //不存在豁免的用户进行关键字排查
                        if(!in_array($user_id,$key_users)){
                            //记录违规关键字消费记录
                            $getDelKey = getSetting('tran_key_name');
                            $key_array = explode(',' , $getDelKey);
                            foreach ($key_array as $k_ka=>$v_ka) {
                                //判断字符串中存在子字符串
                                if (strpos(strtolower($shopping), strtolower($v_ka)) !== false) {
                                    Db::name('user_ice_log')->save([
                                        'user_id'   =>  $user_id,
                                        'card_num'  =>  $card_num,
                                        'type'      =>  0,    //0关键字，1超额
                                        'key_name'  =>  $v_ka,
                                        'amount_auth'   => $amount,
                                        'amount_post'   => $amount,
                                        'bank_type'     => 3,
                                        'card_status'   => $card_status,
                                        'ctime'         => $ctime,
                                        'mcc'           => $mcc
                                    ]);
                                }
                            }
                        }

                        //②减少请求次数，1个小时内推送同一张卡，只执行一次请求
                        $check_cardNum_list = Db::name('api_accept_lss')
                            ->where("id >{$start_id} and card_num='{$card_num}'")
                            ->order('id desc')
                            ->find();
                        $check_time = $check_cardNum_list['ctime'];
                        if (strtotime($log_ctime) - strtotime($check_time) > 60 * 60) {
                            $send_card_detail_param = [
                                "user_id" => $user_id,
                                "card_num" => $card_num,
                                "cardGuid" => $card_token,
                                "action_type" => "getCardDetail"
                            ];
                            $check_card_api = PublicApi::requestApi('Connexpay', $send_card_detail_param);
                            $return_blog = $check_card_api['blog'];
                            $return_list = $check_card_api['list'];
                            if ($return_blog) {
                                $api_amount = $return_list['availableBalance'];
                                $api_amount_total = $return_list['amountLimit'];
                                $api_status = $return_list['status'];
                                if ($api_status == 'Card - Active') {
                                    $api_status = 1;
                                } else if ($api_status == 'Card - Suspended') {
                                    $api_status = 2;
                                } else if ($api_status == 'Card - Deactivated') {
                                    $api_status = 3;
                                }
                                //银行和本地卡状态不一致 更新
                                $new_update_data = [];
                                if ($api_status != $card_status) {
                                    $new_update_data[] = ['status' => $api_status];
                                }
                                //银行与本地卡余额不一致 更新
                                if ((int)$api_amount - (int)$card_balance != 0) {
                                    $new_update_data[] = ['balance' => $api_amount];
                                    Db::name('cards')->where(['id' => $card_id])->update(['balance' => $api_amount]);
                                    Db::name('error_user_action')->save([
                                        'user_id' => $user_id,
                                        'type' => 'accept_3_card_balance_and_status',
                                        'card_num' => $card_num,
                                        'amount' => $card_balance,
                                        'amount_end' => $api_amount,
                                        'amount_limit' => $card_balance_total,
                                        'amount_limit_end' => $api_amount_total,
                                        'remark' => json_encode([
                                            'old_status' => $card_status,
                                            'new_status' => $api_status
                                        ]),
                                        'ctime' => $ctime
                                    ]);
                                }
                                //银3 删卡后 卡限额时1，不过最初的时候更新错误 部分是0
                                $blog_num = 0;
                                if ($api_status == 3) {
                                    $blog_num = 1;
                                }
                                if (abs((int)$api_amount_total) - abs((int)$card_balance_total) != $blog_num) {
                                    Db::name('error_user_action')->save([
                                        'user_id' => $user_id,
                                        'type' => 'accept_3_card_balance_account',
                                        'card_num' => $card_num,
                                        'amount' => $card_balance,
                                        'amount_end' => $api_amount,
                                        'amount_limit' => $card_balance_total,
                                        'amount_limit_end' => $api_amount_total,
                                        'ctime' => $ctime
                                    ]);
                                }
                            }
                        }

                        //asc升序规则，tran_guid第一次出现直接处理,后出现的直接按重复标记
                        switch ($eventType) {
                            case "purchase.card.auth.approved": //auth

                                $insert_auth = [
                                    'user_id'       => $user_id,
                                    'app_code'      => $appCode,
                                    'card_num'      => $card_num,
                                    'tran_status'   => 'Authorized',
                                    'amount_auth'   => $amount,
                                    'reversal_flag' => 'N',
                                    'desc'          => $correctiveAction,
                                    'shopping'      => $shopping,
                                    'ctime'         => $lss_time,
                                    'ctime_real'    => $eventTime,
                                    'bank_type'     => 3,
                                    'tran_token'    => $tran_guid,
                                    'currencyCode'  => $currencyCode,
                                    'country'       => $cardAcceptorCountry,
                                    'mid'           => $mid
                                ];
                                $e = Db::name('cards_trans')->save($insert_auth);

                                //手机短信0预付
                                if (0 - (int)$amount == 0) {
                                    $server_amount = $server_zero_auth;
                                    $shopping_xx = strtolower($shopping);
                                    if (strstr($shopping_xx, 'facebook') || strstr($shopping_xx, 'facebk')) {
                                        $server_amount = $server_facebook;
                                    }
                                    Db::name('server_lss')->save([
                                        'user_id' => $user_id,
                                        'type' => 0,
                                        'card_num' => $card_num,
                                        'tran_amount' => $amount,
                                        'server_amount' => $server_amount,
                                        'ctime' => $lss_time,
                                        'tran_token' => $tran_guid,
                                        'shopping' => $shopping,
                                        'cl_ctime' => $ctime,
                                        'is_action' => 0
                                    ]);
                                }

                                //当发现有大于、等于某个金额M的预授权时，统计这个用户24小时内的预授权总金额，
                                //如果总金额大于、等于N，则冻结这个帐号以及其名下所有的卡，
                                //要求M、N可后台设置，并且可以设置豁免的用户
                                $monitor_tran_auth_amount_user = getSetting('monitor_tran_auth_amount_user');
                                $monitor_user_array = explode(",", $monitor_tran_auth_amount_user);
                                if(!in_array($username, $monitor_user_array)){
                                    $monitor_tran_auth_amount = getSetting('monitor_tran_auth_amount');
                                    if($amount - $monitor_tran_auth_amount >=0){
                                        Db::name('user_ice_log')->save([
                                            'user_id'   =>  $user_id,
                                            'card_num'  =>  $card_num,
                                            'type'      =>  2,    //0关键字，1超额 ,2预扣超额
                                            'amount_auth'   => $amount,
                                            'amount_post'   => $amount,
                                            'bank_type'     => 3,
                                            'card_status'   => $card_status,
                                            'ctime'         => $ctime,
                                            'mcc'           => $mcc
                                        ]);
                                    }
                                }

                                //货币转化费
                                $no_array = ['us', 'usa'];
                                if (!empty($currencyCode)) {
                                    if (strtolower($currencyCode) != 'usd'
                                        || !in_array(strtolower($cardAcceptorCountry), $no_array)
                                    ) {
                                        $new_amount = getSprintf(round(($amount * 1) / 100 + 0.30, 2));
                                        //排查是否有重复的
                                        $check_list = Db::name('code_log')->where(['tran_token' => $tran_guid])->find();
                                        if (empty($check_list)) {
                                            Db::name('code_log')->save([
                                                'user_id' => $user_id,
                                                'amount' => $new_amount,
                                                'tran_amount' => $amount,
                                                'card_num' => $card_num,
                                                'ctime' => $lss_time,
                                                'code' => $currencyCode,
                                                'country' => $cardAcceptorCountry,
                                                'cl_ctime' => $ctime,
                                                'tran_token' => $tran_guid,
                                                'shopping' => $shopping,
                                                'is_action' => 0
                                            ]);
                                        }
                                    }
                                }
                                Db::name('api_accept_lss')->where(['id' => $old_id])
                                    ->update(['is_action' => 1]);
                                break;
                            case "purchase.card.auth.settled": //post
                                $accept_amount = $amount;
                                if($appCode == 'XXXXXX'){
                                    Db::name('error_user_action')->save([
                                        'user_id' => $user_id,
                                        'type' => 'accept_3_appCode_XXXXXX',
                                        'card_num' => $card_num,
                                        'amount' => $card_balance,
                                        'amount_end' => 0,
                                        'amount_limit' => $card_balance_total,
                                        'amount_limit_end' => 0,
                                        'remark' => json_encode([
                                            'appCode' => 'XXXXXX',
                                            'id'      => $old_id
                                        ]),
                                        'ctime' => $ctime
                                    ]);
                                    Db::name('cards_trans')->save([
                                        'user_id'       => $user_id,
                                        'app_code'      => $appCode,
                                        'card_num'      => $card_num,
                                        'tran_status'   => 'Posted',
                                        'amount_auth'   => $amount,
                                        'reversal_flag' => 'N',
                                        'desc'          => $correctiveAction,
                                        'shopping'      => $shopping,
                                        'ctime'         => $lss_time,
                                        'ctime_real'    => $eventTime,
                                        'bank_type'     => 3,
                                        'tran_token'    => $tran_guid,
                                        'currencyCode'  => $currencyCode,
                                        'country'       => $cardAcceptorCountry,
                                        'mid'           => $mid
                                    ]);
                                }else{
                                    $current_tran_list = Db::name('cards_trans')->where([
                                        'app_code'      =>  $appCode,
                                        'card_num'      =>  $card_num,
                                        'bank_type'     =>  3
                                    ])->find();
                                    if($current_tran_list){
                                        $current_tran_id = $current_tran_list['id'];
                                        $tran_status     = $current_tran_list['tran_status'];
                                        if($tran_status == 'Authorized'){
                                            Db::name('cards_trans')->where(['id'=>$current_tran_id])->update([
                                                'tran_status'   => 'Posted',
                                                'amount_post'   => $accept_amount,
                                                'utime'         => $lss_time,
                                                'utime_real'    => $eventTime,
                                            ]);
                                        }
                                    }else{
                                        Db::name('cards_trans')->save([
                                            'user_id'       => $user_id,
                                            'app_code'      => $appCode,
                                            'card_num'      => $card_num,
                                            'tran_status'   => 'Posted',
                                            'amount_auth'   => $amount,
                                            'reversal_flag' => 'N',
                                            'desc'          => $correctiveAction,
                                            'shopping'      => $shopping,
                                            'ctime'         => $lss_time,
                                            'ctime_real'    => $eventTime,
                                            'bank_type'     => 3,
                                            'tran_token'    => $tran_guid,
                                            'currencyCode'  => $currencyCode,
                                            'country'       => $cardAcceptorCountry,
                                            'mid'           => $mid
                                        ]);
                                    }
                                }

                                if ($availableBalance < 0) {
                                    $insert_auth = [
                                        'user_id' => $user_id,
                                        'tran_status' => $eventType,
                                        'card_num' => $card_num,
                                        'card_status' => $card_status,
                                        'amount' => $accept_amount,
                                        'availableBalance' => $availableBalance,
                                        'tran_token' => $tran_guid,
                                        'ctime' => $lss_time,
                                        'shopping' => $shopping
                                    ];
                                    Db::name('cards_trans_fz')->save($insert_auth);
                                    EmailSend::sendGridEmail('bank_3_to_amount' , [
                                        'email'     => "sensir267@gmail.com",
                                        'body'      => "管理员您好，账号:{$username}<br />
                                                被强制扣款,卡号:{$card_num},金额:{$accept_amount},卡余额:{$availableBalance},<br />
                                                vvacard运营小组<br />",
                                        'title' => "账号({$username}-{$card_num}-{$accept_amount}) - 被强制扣款(银3) -- CardEcho",
                                    ]);
                                    EmailSend::sendGridEmail('bank_3_to_amount' , [
                                        'email'     => "897194@gmail.com",
                                        'body'      => "管理员您好，账号:{$username}<br />
                                                被强制扣款,卡号:{$card_num},金额:{$accept_amount},卡余额:{$availableBalance},<br />
                                                vvacard运营小组<br />",
                                        'title' => "账号({$username}-{$card_num}-{$accept_amount}) - 被强制扣款(银3) -- CardEcho",
                                    ]);
                                }

                                //记录实际扣款($) >预扣款($)*1.05 , 1.05可在配置修改
                                $getAmountPost = getSetting('amount_post_limit');
                                if (!empty($accept_amount)) {
                                    if ($accept_amount - ($accept_amount * $getAmountPost) > 0) {
                                        Db::name('user_ice_log')->save([
                                            'user_id' => $user_id,
                                            'card_num' => $card_num,
                                            'type' => 1,    //0关键字，1超额
                                            'amount_auth' => $accept_amount,
                                            'amount_post' => $accept_amount,
                                            'bank_type' => 3,
                                            'card_status' => $card_status,
                                            'ctime' => $ctime,
                                            'mcc' => $mcc
                                        ]);
                                    }
                                }
                                Db::name('api_accept_lss')->where(['id' => $old_id])
                                    ->update(['is_action' => 1]);
                                break;
                            case "purchase.card.auth.declined":
                                $accept_amount = $amount;
                                if($shopping != 'Visa Provisioning Service'){
                                    $decline_msg = '';
                                    $card_num4 = substr($card_num, 0, 6) . ' *** ' . substr($card_num, -4);
                                    if ($decline_error == 'No sufficient funds') {
                                        $decline_msg = "我们注意到您的卡片 ({$card_num4}) 由于余额不足导致消费失败,请补充余额后再消费,您可以点卡号旁边的‘详情’进入卡片操作页面，增加卡片余额。账户余额和卡片余额是分开的，充值到卡上才能正常消费.";
                                    } else if ($decline_error == 'Wrong exp.date') {
                                        $decline_msg = "我们注意到您的卡片 ({$card_num4}) 由于输入了错误的过期时间导致消费失败，请输入正确的过期时间.";
                                    } else if ($decline_error == 'Card is not active') {
                                        $decline_msg = "我们注意到您的卡片({$card_num4})由于处于冻结状态导致消费失败，请激活后再消费。请在卡列表，冻结再激活该卡.如果卡片已经删除，请取消您的订阅，防止因发生拒付而产生服务费.";
                                    } else if ($decline_error == 'MCC not allowed') {
                                        $decline_msg = "高风险商户，银行拒绝交易";
                                    }
                                    if (!empty($decline_msg)) {
                                        $email_list = Db::name('api_email')
                                            ->where([
                                                'user_id' => $user_id,
                                                'card_num' => $card_num
                                            ])->order('ctime desc')->find();
                                        $email_ctime = $email_list['ctime'] ?? 0;
                                        //每超过24小时再发送邮件
                                        if ($ctime - $email_ctime > 24 * 60 * 60) {
                                            $username = Db::name('user')->where(['id' => $user_id])->value('username');

                                            $shopping_xx = strtolower($shopping);
                                            if (strstr($shopping_xx, 'facebook') || strstr($shopping_xx, 'facebk')) {
                                                $amount_rate = 0.3;
                                            } else {
                                                $amount_rate = 0.2;
                                            }
                                            DecEmailSend::sendGridDecEmail([
                                                'user_id' => $user_id,
                                                'card_num' => $card_num,
                                                'email' => $username,
                                                'amount' => $accept_amount,
                                                'amount_rate' => $amount_rate,
                                                'desc' => "{$username} 您好 ; 您有在商户({$shopping})的拒付, 金额为{$accept_amount} , {$decline_msg}",
                                                'title' => "您的卡片({$card_num4})有一笔 金额" . $accept_amount . "拒付.请核查拒付原因 -- CardEcho",
                                            ]);
                                        }
                                    }

                                    $decline_server = $server_declined;
                                    $decline_type = 1;
                                    $shopping_xx = strtolower($shopping);
                                    if (strstr($shopping_xx, 'facebook') || strstr($shopping_xx, 'facebk')) {
                                        $decline_server = $server_facebook;
                                        $decline_type = 2;
                                    }
                                    //2024_12_18拒付新规则:系数(0.2,face0.3) + 拒付金额*1%
                                    $dec_rate = Db::name('user')->where(['id' => $user_id])->value('dec_rate');
                                    $decline_server = getSprintf($decline_server + round($accept_amount * $dec_rate, 2));

                                    //排查是否有重复的
                                    $check_list = Db::name('server_lss')->where(['tran_token' => $tran_guid])->find();
                                    if (empty($check_list)) {
                                        Db::name('server_lss')->save([
                                            'user_id' => $user_id,
                                            'type' => $decline_type,
                                            'card_num' => $card_num,
                                            'tran_amount' => $accept_amount,
                                            'server_amount' => $decline_server,
                                            'ctime' => $lss_time,
                                            'tran_token' => $tran_guid,
                                            'shopping' => $shopping,
                                            'cl_ctime' => $ctime,
                                            'is_action' => 0
                                        ]);
                                    }
                                }

                                $save_param = [
                                    'user_id'       => $user_id,
                                    'app_code'      => $appCode,
                                    'card_num'      => $card_num,
                                    'tran_status'   => 'Declined',
                                    'amount_auth'   => $accept_amount,
                                    'reversal_flag' => 'N',
                                    'shopping'      => $shopping,
                                    'ctime'         => $lss_time,
                                    'ctime_real'    => $eventTime,
                                    'bank_type'     => 3,
                                    'tran_token'    => $tran_guid,
                                    'desc'          => $decline_error
                                ];
                                Db::name('cards_trans')->save($save_param);

                                Db::name('api_accept_lss')->where(['id' => $old_id])
                                    ->update(['is_action' => 1]);
                                break;
                            case "purchase.card.auth.reversal.settled":
                                if((int)$amount - 0 != 0){
                                    Db::name('cards_trans')->save([
                                        'user_id'       => $user_id,
                                        'app_code'      => $appCode,
                                        'card_num'      => $card_num,
                                        'tran_status'   => 'Reversal',
                                        'amount_auth'   => $amount,
                                        'reversal_flag' => 'N',
                                        'desc'          => $correctiveAction,
                                        'shopping'      => $shopping,
                                        'ctime'         => $lss_time,
                                        'ctime_real'    => $eventTime,
                                        'bank_type'     => 3,
                                        'reversal_expired'  => 0,
                                        'tran_token'    => $tran_guid,
                                    ]);
                                    Db::name('api_accept_lss')->where(['id' => $old_id])
                                        ->update(['is_action' => 1]);
                                }else{
                                    Db::name('api_accept_lss')->where(['id' => $old_id])
                                        ->update(['is_action' => 4]);
                                }

                                break;
                            case "purchase.card.auth.reversal.issuerexpiration.settled":
                                if((int)$amount - 0 != 0){
                                    Db::name('cards_trans')->save([
                                        'user_id'       => $user_id,
                                        'app_code'      => $appCode,
                                        'card_num'      => $card_num,
                                        'tran_status'   => 'Reversal',
                                        'amount_auth'   => $amount,
                                        'reversal_flag' => 'N',
                                        'desc'          => $correctiveAction,
                                        'shopping'      => $shopping,
                                        'ctime'         => $lss_time,
                                        'ctime_real'    => $eventTime,
                                        'bank_type'     => 3,
                                        'reversal_expired'  => 1,
                                        'tran_token'    => $tran_guid,
                                    ]);
                                    Db::name('api_accept_lss')->where(['id' => $old_id])
                                        ->update(['is_action' => 1]);
                                }else{
                                    Db::name('api_accept_lss')->where(['id' => $old_id])
                                        ->update(['is_action' => 4]);
                                }
                                break;
                            case "purchase.card.return.auth.settled":
                                $insert_credit = [
                                    'user_id'       => $user_id,
                                    'app_code'      => $appCode,
                                    'card_num'      => $card_num,
                                    'tran_status'   => "Credit",
                                    'amount_auth'   => $amount,
                                    'reversal_flag' => 'N',
                                    'shopping'      => $shopping,
                                    'ctime'         => $lss_time,
                                    'ctime_real'    => $eventTime,
                                    'amount_post'   => $amount,
                                    'utime'         => $lss_time,
                                    'utime_real'    => $eventTime,
                                    'bank_type'     => 3,
                                    'tran_token'    => $tran_guid
                                ];
                                Db::name('cards_trans')->save($insert_credit);

                                $abs_amount = abs($amount);
                                $credit_add = [
                                    'card_num'      => $card_num,
                                    'amount'        => $abs_amount,
                                    'ctime'         => $lss_time,
                                    'app_code'      => $appCode,
                                    'user_id'       => $user_id,
                                    'shopping'      => $shopping,
                                    'tran_token'    => $tran_guid,
                                    'card_status'   => $card_status
                                ];
                                if($card_status == 3){
                                    //2025-0925
                                    //卡删除时，银行推送的余额可能有误，再请求次卡余额进行更新
                                    $send_param         = [
                                        "user_id"       => $user_id,
                                        "card_num"      => $card_num,
                                        "cardGuid"      => $card_token,
                                        "action_type"   => "getCardDetail"
                                    ];
                                    $check_card_api = PublicApi::requestApi('Connexpay',$send_param);
                                    $return_blog    = $check_card_api['blog'];
                                    $return_list    = $check_card_api['list'];
                                    if($return_blog){
                                        $api_amount         = $return_list['availableBalance'];
                                        Db::name('cards')->where(['id' => $card_id])
                                            ->update(['balance' => $api_amount]);
                                    }
                                }else{
                                    $credit_result = CardsTransCredit::endCredit([
                                        'card_num'      =>  $card_num,
                                        'user_id'       =>  $user_id,
                                        'credit_amount' =>  $abs_amount,
                                        'shopping'      =>  $shopping,
                                    ] , 2);

                                    if($credit_result){
                                        $credit_server_set = UserSet::getCardSet($user_id);
                                        $card_type         = substr($card_num , 0,6);
                                        $credit_server_set_rate = $credit_server_set[3][$card_type]['rate_refund'];
                                        $credit_server_amount = getSprintf(round($abs_amount * $credit_server_set_rate,2));
                                        $credit_add += [
                                            'credit_status' => 1,
                                            'utime' => getUtcTime(),
                                            'server_amount' => $credit_server_amount
                                        ];
                                    }
                                }

                                Db::name('cards_trans_credit_lss')->save($credit_add);

                                Db::name('api_accept_lss')->where(['id' => $old_id])
                                    ->update(['is_action' => 1]);
                                break;
                            default:
                                Db::name('api_accept_lss')->where(['id' => $old_id])
                                    ->update(['is_action' => 4]);
                                break;
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        Db::name('api_accept_lss')->where(['id' => $old_id])
                            ->update(['is_action' => 2]);
                        Db::name('error_user_action')->save([
                            'user_id' => $user_id,
                            'type' => 'accept_3_action_sql_error',
                            'card_num' => $card_num,
                            'amount' => 0,
                            'amount_end' => 0,
                            'amount_limit' => 0,
                            'amount_limit_end' => 0,
                            'remark' => json_encode([
                                'accept_lss_id' => $old_id,
                            ]),
                            'desc'=>json_encode($e->getMessage()),
                            'ctime' => $ctime
                        ]);
                        continue;
                    }
                }
            } catch (\Exception $e) {
                Db::name('api_accept_lss')->where(['id' => $old_id])
                    ->update(['is_action' => 2]);
                //记录错误失败的记录id
                Db::name('error_user_action')->save([
                    'user_id' => $user_id,
                    'type' => 'accept_3_action_error',
                    'card_num' => $card_num,
                    'amount' => 0,
                    'amount_end' => 0,
                    'amount_limit' => 0,
                    'amount_limit_end' => 0,
                    'remark' => json_encode([
                        'accept_lss_id' => $old_id,
                    ]),
                    'desc'=>json_encode($e->getMessage()),
                    'ctime' => $ctime
                ]);
            }
        }
    }

}
